# Create your views here.
from django.db.models import Q
from django.forms import model_to_dict
from rest_framework.views import APIView
from components.models import SysMenu, SysCode
from general.Transform import list_to_tree
from utils.views import Result

class SysCodeManager(APIView):
    def get(self, request):
        data = request.query_params
        if data.get('sys_code_type'):
            sys_menu = SysCode.objects.filter(~Q(status=0)).filter(sys_code_type=data['sys_code_type']).values()
            sys_menu_data = list(sys_menu)
            return Result.success_response(data=sys_menu_data, msg='查询成功')
        else:
            sys_menu = SysMenu.objects.filter(~Q(status=0)).values()

        sys_menu_data = list(sys_menu)

        data = list_to_tree(sys_menu_data)

        return Result.success_response(data=data, msg='查询成功')

    def post(self, request):
        data = request.query_params

        sys_menu = SysCode.objects.filter(sys_code_type="package_type").values()

        sys_menu_data = list(sys_menu)
        returnData = []
        for item in sys_menu_data:
            returnData.append({"value": item.get("sys_code"),"label": item.get("sys_code_name")})

        return Result.success_response(data=returnData, msg='查询成功')
